package com.lfg.leetcode.leetcode0;


public class No19RemoveNthFromEnd {

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode node = head;
        int length = 0;
        while (node != null) {
            node = node.next;
            length++;
        }
        int i = length - n;
        if (i < 0) {
            return null;
        }
        node = head;
        if (i == 0) {
            return head.next;
        }
        while (i-- > 0) {
            if (i > 0) {
                node = node.next;
                continue;
            }
            if (node.next != null) {
                node.next = node.next.next;
            } else {
                node.next = null;
            }
        }
        return head;
    }


    public static void main(String[] args) {
        System.out.println(removeNthFromEnd(ListNode.deserialize("[1,2,3,4,5,6,7,8,9]"), 1).val);
//        System.out.println(threeSum(new int[]{-2,0,0,2,2}));
    }
}
